package com.example.demo.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * ⑤  时间加减的测试类
 *
 * @author luowenjie
 * @since 2022/3/29 11:09
 */
public class Date5Test {

    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        //1.过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = format.format(d);
        System.out.println("过去七天："+day);
        //2.过去一月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        System.out.println("过去一个月："+mon);
        //3.过去三个月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -3);
        Date m3 = c.getTime();
        String mon3 = format.format(m3);
        System.out.println("过去三个月："+mon3);
        //4.过去一年
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        System.out.println("过去一年："+year);
        //5.现在时间24小时制
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate=format1.format(new Date());
        System.out.println(nowDate);
        //6.当天的开始时间
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c2 = Calendar.getInstance();
        String start = format2.format(c2.getTime())+" 00:00:00";
        System.out.println(start);
        //7.当天的截止时间
        SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        String end = format3.format(calendar.getTime())+" 23:59:59";
        System.out.println(end);
        //8.当前时间的前一个星期 开始时间
        SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c4 = Calendar.getInstance();
        c4.add(Calendar.DAY_OF_MONTH, -6);
        String start4 = format4.format(c.getTime())+" 00:00:00";
        System.out.println(start4);
        //9.当天日期前一个月
        SimpleDateFormat format5 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c5 = Calendar.getInstance();
        c5.add(Calendar.MONTH, -1);    //得到前一个月
        String start5 = format5.format(c.getTime())+" 00:00:00";
        System.out.println(start5);
        //10.当前时间的前一年开始时间
        SimpleDateFormat format6 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c6 = Calendar.getInstance();
        c6.add(Calendar.YEAR, -1); //年份减1
        String start6 =format6.format(c.getTime())+" 00:00:00";
        System.out.println(start6);
        //11.当前时间的周一时间和周末时间 setfirstdayofweek（）方法
        SimpleDateFormat format7  = new SimpleDateFormat("YYYY-MM-dd ");
        Calendar c7 = Calendar.getInstance();
        c7.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        String weekStart7 = format7.format(c.getTime())+" 00:00:00";
        System.out.println(weekStart7);
        Calendar ca = Calendar.getInstance();
        ca.setFirstDayOfWeek(Calendar.MONDAY);
        ca.set(Calendar.DAY_OF_WEEK, ca.getFirstDayOfWeek() + 6); // Sunday
        String weekEnd = format.format(ca.getTime())+" 23:59:59";
        System.out.println(weekEnd);
        //12.当前时所在月份的第一天和最后一天
        SimpleDateFormat format8=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c8 = Calendar.getInstance();
        c8.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String monthStart = format8.format(c.getTime())+" 00:00:00";
        System.out.println(monthStart);
        Calendar ca8 = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca8.getActualMaximum(Calendar.DAY_OF_MONTH));
        String monthEnd = format.format(ca8.getTime())+" 23:59:59";
        System.out.println(monthEnd);
        //13.当期时间所在年份的开始时间
        SimpleDateFormat format9 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c9 = Calendar.getInstance();
        c9.set(c.get(Calendar.YEAR) ,0, 1);//开始时间日期
        String yearStart = format9.format(c.getTime())+" 00:00:00";
        System.out.println(yearStart);
        Calendar ca9 = Calendar.getInstance();
        ca9.set(ca.get(Calendar.YEAR) ,11, ca9.getActualMaximum(Calendar.DAY_OF_MONTH));//结束日期
        String yearEnd = format.format(ca9.getTime())+" 23:59:59";
        System.out.println(yearEnd);
        //14.计算时间差取得两个时间的微秒级的时间差
        Calendar c10 = Calendar.getInstance();
        Calendar cEnd10 = Calendar.getInstance();
        cEnd10.add(Calendar.DATE,1);
        //得微秒级时间差
        long val = cEnd10.getTimeInMillis() - c10.getTimeInMillis();
        //换算后得到天数
        long day10 = val / (1000 * 60 * 60 * 24);
        //15.获取某个时间的上周一和周日
        Calendar cal = Calendar.getInstance();
//n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
        int n = 1;
        String monday;
        cal.add(Calendar.DATE, n*7);
//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        System.out.println(monday);
    }

}
